﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace The_LarX
{
    public class Node_sgAnimation : VisualScript_Node, IVisualScriptNode_NeedObject
    {
        public List<VisualScript_Node> Objects { get; set; }

        public Node_sgAnimation(VisualScript vs)
            : base(vs)
        {
            this.RepresentationType = typeof(VSR_ObjectNeeded);
            Objects = new List<VisualScript_Node>();
            Load();
        }

        public Node_sgAnimation()
        {
            this.RepresentationType = typeof(VSR_ObjectNeeded);
            Load();
        }

        private void Load()
        {
            this.descriptionName = "sgMotion Player";
        }

        public bool CrossFade { get; set; }
        public string AnimationName { get; set; }
        public float Time { get; set; }

        public override void Draw()
        {
            if (AllConditionsAllow())
            {
                base.Draw();

                if (!string.IsNullOrEmpty(AnimationName))
                {
                    for (int i = 0; i < Objects.Count; i++)
                    {
                        if (Objects[i] is IVisualScriptNode_Object)
                        {
                            IVisualScriptNode_Object obj = Objects[i] as IVisualScriptNode_Object;
                            if (obj.Object is sgMotionAnimatedActor)
                            {
                                sgMotionAnimatedActor actor = obj.Object as sgMotionAnimatedActor;
                                if (CrossFade)
                                {
                                    actor.AnimationController.CrossFade(actor.SkinnedModel.AnimationClips[AnimationName],
                                        TimeSpan.FromSeconds(Time));
                                }
                                else
                                {
                                    actor.AnimationController.PlayClip(actor.SkinnedModel.AnimationClips[AnimationName]);
                                }
                            }
                        }
                    }
                }
            }
        }

        public override void SerializeData(SerializeData Data)
        {
            base.SerializeData(Data);

            Data.AddData(CreatedNumber + "Objects", NodesToStringWithNames(Objects));
            Data.AddData(CreatedNumber + "Crossfade", this.CrossFade);
            Data.AddData(CreatedNumber + "AnimationName", this.AnimationName);
            Data.AddData(CreatedNumber + "Time", this.Time);
        }
        public override void DeserializeData(SerializeData Data)
        {
            base.DeserializeData(Data);

            this.Objects = StringWithNamesToNodes(Data.GetData<string>(CreatedNumber + "Objects"), Script);
            this.CrossFade = Data.GetData<bool>(CreatedNumber + "Crossfade");
            this.AnimationName = Data.GetData<string>(CreatedNumber + "AnimationName");
            this.Time = Data.GetData<float>(CreatedNumber + "Time");
        }
    }
}
